package com.taptech.doufu.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.taptech.doufu.R
import com.taptech.doufu.constant.Constant
import com.taptech.doufu.model.novel.bean.NovelRecommendBean
import com.taptech.doufu.ui.activity.NovelDetailsActivity
import com.taptech.doufu.util.GlideUtil
import com.taptech.doufu.util.TagsUtil
import kotlinx.android.synthetic.main.topic_nomal_item.view.*

class NovelRecommendAdapter(context:Context): BaseRecyclerAdappter<NovelRecommendBean,NovelRecommendAdapter.ViewHolder>(context){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.topic_nomal_item,null)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val item = dataList[position]
        holder.itemView.title.text = item.topic_title
        holder.itemView.tvDesc.text = item.description
        holder.itemView.tvAuthorDesc.text = item.user.nickname
        GlideUtil.displayImage(holder.itemView.ivPic, if (item.with_image.startsWith("http")) item.with_image else "https://imgold.doufu.la/${item.with_image}",R.drawable.img_default_loading_l)

        val tags = item.tags2.split(" ")
        if (tags.isNotEmpty() && tags[0].isNotEmpty()) {
            holder.itemView.tvTag1.visibility = View.VISIBLE
            holder.itemView.tvTag1.setOnClickListener {
                TagsUtil.openWeexTagActivity(mContext, tags[0], Constant.OBJECT_TYPE_NOVEL, "")
            }
            holder.itemView.tvTag1.text = tags[0]
            if (tags.size > 1) {
                holder.itemView.tvTag2.visibility = View.VISIBLE
                holder.itemView.tvTag2.setOnClickListener {
                    TagsUtil.openWeexTagActivity(mContext, tags[1], Constant.OBJECT_TYPE_NOVEL, "")
                }
                holder.itemView.tvTag2.text = tags[1]
            }else{
                holder.itemView.tvTag2.visibility = View.GONE
            }
        }else{
            holder.itemView.tvTag1.visibility = View.GONE
            holder.itemView.tvTag2.visibility = View.GONE
        }

        holder.itemView.setOnClickListener {
            NovelDetailsActivity.startActivity(mContext,item.id)
        }
    }

    class ViewHolder(view: View): RecyclerView.ViewHolder(view)
}